package cn.wolfcode.mq;

import ch.qos.logback.core.util.TimeUtil;
import cn.wolfcode.config.WebSocketServer;
import com.alibaba.fastjson.JSON;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.stereotype.Component;

/**
 * @author LuoRis
 * @date 2021/03/08 18:43
 * @description 接收orderResult
 */
@RocketMQMessageListener(
        topic = MQConstant.ORDER_RESULT_TOPIC,
        consumerGroup = "MQOrderResultListener"
)
@Component
@Slf4j
public class MQOrderResultListener implements RocketMQListener<OrderMQResult> {

    @SneakyThrows
    @Override
    public void onMessage(OrderMQResult orderMQResult) {

        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //接收订单队列信息发送到前端页面
        WebSocketServer webSocketServer = WebSocketServer.clients.get(orderMQResult.getToken());
        webSocketServer.getSession().getBasicRemote().sendText(JSON.toJSONString(orderMQResult));
    }
}
